// 客服消息
var fs = require('fs')

// 读取文件中有没有token，有则返回token
var Promise = require('bluebird')
var request = Promise.promisify(require('request'))


exports.uploadTempMedia = function(api,accessToken,type,filepath) {
    var that = this;
    var form = { //构造表单
        media: fs.createReadStream(filepath)
    }
    return new Promise(function(resolve, reject) {

        var url = api.uploadTempMedia + accessToken + '&type=' + type;
        request({ url: url, method: 'POST', formData: form, json: true }).then(function(response) {
            var _data = response.body;
            if (_data) {
                resolve(_data)
            } else {
                throw new Error('upload material failed!');
            }
        }).catch(function(err) {
            reject(err);
        });
    });
}